package com.crazymaker.springcloud.websocket.processer;

import com.crazymaker.springcloud.common.dto.UserDTO;
import com.crazymaker.springcloud.common.util.JsonUtil;
import com.crazymaker.springcloud.websocket.session.ServerSession;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 业务处理器
 * Created by 尼恩 @ 疯狂创客圈
 */
@Slf4j
public class MsgProcesser {
    private static final Logger logger = LoggerFactory.getLogger(MsgProcesser.class);


    /**
     * 单例
     */
    private static MsgProcesser singleInstance = new MsgProcesser();

    public static MsgProcesser getSingleton() {
        return singleInstance;
    }

    /**
     * 连接建立成功调用的方法
     *
     * @param s 会话
     */
    public String onOpen(ServerSession s) throws IOException {
        Map<String, String> result = new HashMap<>();
        result.put("type", "system");
        result.put("content", "恭喜，已经登陆成功");
        result.put("account", s.getUser().getUsername());

        String json = JsonUtil.pojoToJson(result);
        return json;
    }

    /**
     * 连接关闭调用的方法
     */
    public String onClose(ServerSession s) {
        UserDTO user = s.getUser();
        if (user != null) {
            String nick = user.getNickname();
            Map<String, String> result = new HashMap<>();
            result.put("type", "init");
            result.put("msg", nick + "离开房间");
            result.put("sendUser", "系统消息");
            String json = JsonUtil.pojoToJson(result);
            return json;
        }
        return null;
    }

    /**
     * 系统错误的消息
     */
    public String onSystemError(ServerSession s) {
        UserDTO user = s.getUser();
        if (user != null) {
            String account = user.getUsername();
            Map<String, String> result = new HashMap<>();
            result.put("type", "close");
            result.put("msg",  " 系统错误 ");
            result.put("account", user.getUsername());
            String json = JsonUtil.pojoToJson(result);
            return json;
        }
        return null;
    }
 /**
     * 空闲太久的消息
     */
    public String onIdleTooLong(ServerSession s) {
        UserDTO user = s.getUser();
        if (user != null) {
            String account = user.getUsername();
            Map<String, String> result = new HashMap<>();
            result.put("type", "close");
            result.put("msg", account + " 空闲太久 ");
            result.put("account", user.getUsername());
            String json = JsonUtil.pojoToJson(result);
            return json;
        }
        return null;
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 消息内容
     * @param session 会哈
     */
    public String onMessage(String message, ServerSession session) {
        TypeToken typeToken = new TypeToken<HashMap<String, String>>() {
        };
        Map<String, String> map = JsonUtil.jsonToPojo(message, typeToken.getType());
        Map<String, String> result = new HashMap<>();
        if (null != map.get("type") && map.get("type").equals("msg")) {
            result.put("type", "msg");
            result.put("msg", map.get("msg"));
            result.put("from", session.getUser().getUsername());
            String json = JsonUtil.pojoToJson(result);
            return json;
        }
        return null;
    }


}